<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Bank extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper('bantuan_helper');
		$this->load->model('Bank_model');
		if(!$this->session->userdata('user_id') && !$this->session->userdata('user_display_name') ):
			redirect('backend');
		elseif($this->session->userdata('user_rule')=='Sales'):
			redirect('backend/no_acces');
		endif;

	}

	function index()
	{
		$this->load->library('pagination');
		$offset = $this->uri->segment(4);
		$config['base_url'] = site_url().'/backend/bank/index';
		$config['total_rows'] = count($this->Bank_model->getBank('all',FALSE,FALSE,FALSE));
		$config['per_page'] = $this->config->item('per_page');
		$config['uri_segment'] = '4';
		
		$data['urut'] = $this->uri->segment(4);
		$data['title'] = 'Bank';
		$data['template'] = 'bank/index';
		$data['res'] = $this->Bank_model->getBank('all',FALSE,$config['per_page'],$offset);
		$this->pagination->initialize($config);
		$this->load->view('backend/index',$data);
	}

	function add()
	{
		$this->form_validation->set_rules('nama_rek', 'Rekening Atas Nama','required');
		$this->form_validation->set_rules('nama_bank', 'Nama Bank','required');
		$this->form_validation->set_rules('no_rek', 'No Rekening','required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['title'] = 'Bank';
			$data['template'] = 'bank/add';
			$this->load->view('backend/index',$data);
		}
		else
		{
			$data = array
				(
				'nama_rek'=>$this->input->post('nama_rek'),
				'nama_bank'=>$this->input->post('nama_bank'),
				'no_rek'=>$this->input->post('no_rek'),
				'alamat_bank'=>$this->input->post('alamat_bank')
				);
			$this->Bank_model->addBank($data);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Satu data Berhasil ditambahkan</div>');
			redirect('backend/bank');
		}	
	}

	function edit()
	{
		$this->form_validation->set_rules('nama_rek', 'Rekening Atas Nama','required');
		$this->form_validation->set_rules('nama_bank', 'Nama Bank','required');
		$this->form_validation->set_rules('no_rek', 'No Rekening','required');

		if ($this->form_validation->run() == FALSE)
		{
			$id = $this->uri->segment(4);
			$data['title'] = 'Bank';
			$data['template'] = 'bank/edit';
			$data['row'] = $this->Bank_model->getBank(FALSE,$id,FALSE,FALSE);
			$this->load->view('backend/index',$data);
		}
		else
		{
			$id = $this->uri->segment(4);
			$data = array
					(
					'nama_rek'=>$this->input->post('nama_rek'),
					'nama_bank'=>$this->input->post('nama_bank'),
					'no_rek'=>$this->input->post('no_rek'),
					'alamat_bank'=>$this->input->post('alamat_bank')
					);
			$this->Bank_model->editBank($id,$data);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data berhasil diupdate</div>');
			redirect('backend/bank/edit/'.$id);
		}
			
	}

	function delete($id)
	{
		$id = $this->uri->segment(4);
		$uri = $this->uri->segment(5);
		$this->Bank_model->deleteBank($id);
		$count = count($this->Bank_model->getBank('all',FALSE,FALSE,FALSE));
		$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data berhasil dihapus</div>');
		getPage('bank','delete',$uri,$count);
	}
	
	function ajax_bulk_action()
	{
		foreach($_POST["bank_id"] as $id):
			$this->db->where('bank_id',$id);
			$this->db->delete('tb_bank');
		endforeach;
	}

}